/**
 * 
 */
package repository.relsys;

import java.util.Collection;


import java.util.HashMap;
import java.util.LinkedList;

import java.util.Map;

import org.apache.log4j.Logger;


import repository.relsys.app.Settings;
import repository.relsys.config.Property;
import repository.relsys.domainmodel.ltool.interfaces.Blog;
import repository.relsys.domainmodel.ltool.interfaces.ContactList;
import repository.relsys.domainmodel.ltool.interfaces.Feature;
import repository.relsys.domainmodel.ltool.interfaces.GeneralInformation;
import repository.relsys.domainmodel.ltool.interfaces.LearningTool;
import repository.relsys.domainmodel.ltool.interfaces.TechnicalInformation;
import repository.relsys.domainmodel.ltool.pojos.MobileDeviceCls;
import repository.relsys.domainmodel.ltool.pojos.OperetingSystemCls;
import repository.relsys.domainmodel.ltool.pojos.ToolAvailabilityCls;
import repository.relsys.domainmodel.ltool.pojos.ToolCostCls;

import repository.relsys.services.DataModelManager;

import repository.relsys.services.RelsysFileExportImport;
import repository.relsys.services.spring.RelsysResourceFactory;

public class RelsysTest2 {

	private static Logger logger = Logger.getLogger(RelsysTest2.class.getName());

	public static void main(String[] args) {
		try {
			// read settings from config.xml
			
			Settings settings = Settings.getInstance();

		

			// add Context Params for DB connection
			Map<String, String> contextParams = new HashMap<String, String>();
			contextParams.put("db.url", settings.config.dbConfig.dbURL);
			contextParams.put("db.user", settings.config.dbConfig.dbUser);
			contextParams.put("db.password",
					settings.config.dbConfig.dbPassword);

			for (Property property : settings.config.sdbConfig.sdbaseConfig)
			{
				contextParams.put(property.key, property.value);
			}
			

			// Just for testing now - fill the mock-up database
			RelsysTest2.initRepository();
			logger.info("Repository initialised!");
			// stop if RETURN is written in console
			System.in.read();

			// synch data model with file or db
			DataModelManager.getInstance().flush();

		
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
	}

	public static void initRepository() throws Exception {

		RelsysResourceFactory rf = new RelsysResourceFactory();
		
	
       RelsysFileExportImport rfexim = new RelsysFileExportImport();
         
		
		LearningTool ltool = (LearningTool) rf
				.createResource(LearningTool.class);
		ltool.setTitle("WordPress");

		GeneralInformation gi = (GeneralInformation) rf
				.createResource(GeneralInformation.class);

		gi.setDescription("WordPress is ...");
		gi.setToolURL("http://www.wordpress.com");
		gi.setToolCost(ToolCostCls.FREE);
		gi.setToolAvailability(ToolAvailabilityCls.HOSTED);

		ltool.setGeneralInfo(gi);

		TechnicalInformation ti = (TechnicalInformation) rf
				.createResource(TechnicalInformation.class);
		ti.setLaunchedTime("12-01-2001");
		ti.setVersion("2.9");
		ti.registration(true);
		ti.isOpenSource(false);
		ti.addMD(MobileDeviceCls.BLACKBERRY);
		ti.addMD(MobileDeviceCls.ANDROID);
		ti.addMD(MobileDeviceCls.IPHONE);
		ti.addOS(OperetingSystemCls.LINUX);
		ti.addOS(OperetingSystemCls.WINDOWS);
		ti.addOS(OperetingSystemCls.MAC);

		ltool.setTechnicalInfo(ti);

		Collection<Feature> features = new LinkedList<Feature>();
		Blog blog = (Blog) rf.createResource(Blog.class);
		ContactList folowers = (ContactList) rf
				.createResource(ContactList.class);
		features.add(blog);
		features.add(folowers);

		ltool.setFeatures(features);

		rfexim.saveResource(ltool,true);
		
		
	}



}
